// pages/student/mine/center/mine.js
var app = getApp();
import { post } from "../../../../asserts/js/request";
import {student_active} from "../../../../asserts/js/common/barList";
import {initStudentInfo} from "../../../../asserts/js/common/initInof";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barList: student_active(2),
    headImg: "",
    top: app.globalData.bottom + 'px',
    userInfoList: [],
    operateList: [
      {
        src: "/asserts/imgs/operation/mine-record.png",
        name: "浏览记录"
      },
      {
        src: "/asserts/imgs/operation/mine-scan.png",
        name: "扫一扫"
      },
      {
        src: "/asserts/imgs/operation/mine-about.png",
        name: "关于我们"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.userInfo)
    let this_ = this
    // this.initInfo();

    initStudentInfo().then(data => {
      this_.initInfo();

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  operateChoose: function(e) {
    console.log(e);
    let this_ = this;
    if (e.detail.name == "浏览记录") {
      wx.navigateTo({
        url: '/pages/student/mine/history/history',
      })
      return
    }
    if (e.detail.name == "扫一扫") {
      if(app.globalData.userInfo) {
        wx.scanCode({
          onlyFromCamera: true,
          success (res) {
            console.log("扫一扫触发加入班级");
            console.log(res)
            var result = res.result;
            post("studentSignInClass",{
              openId: app.globalData.openId,
              nickName: app.globalData.userInfo.nickName,
              classId: result
            }).then(data => {
              console.log(data);
              wx.showToast({
                title: '已提交申请，等待审核',
                icon: "none"
              })
            })
          }
        })
      }else{
        wx.showModal({
          title: '授权',
          content: '加入班级需要获取您的昵称和头像，是否前往授权？',
          success (res) {
            if (res.confirm) {
              wx.openSetting({
                withSubscriptions: true,
                success: function(e) {
                  console.log()
                  if(e.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                      success: res => {
                        app.globalData.userInfo = res.userInfo;
                        initStudentInfo().then(data => {
                          this_.initInfo();
                          wx.showToast({
                            title: '您现在可以使用扫一扫加入班级了',
                            icon: "none"
                          })
                        })
                      }
                    })
                  }else {
                    wx.showToast({
                      title: '未授权无法加入班级',
                      icon: 'none'
                    })
                  }
                }
              })
            } else if (res.cancel) {
              wx.showToast({
                title: '未授权无法加入班级',
                icon: 'none'
              })
            }
          }
        })
      }
    }
  },
  initInfo: function() {
    this.setData({
      userInfoList: [{
        key: 'Id',
        value: app.globalData.studentInfo.studentId
      },
      {
        key: '姓名',
        value: app.globalData.studentInfo.studenNickName
      }],
      headImg: app.globalData.userInfo?app.globalData.userInfo.avatarUrl:"/asserts/imgs/common/default-head-img.png"
    })
  }
})